package com.zzq.my.jetpacks.jectpackapp.adapter;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.zzq.my.jetpacks.jectpackapp.R;
import com.zzq.my.jetpacks.jectpackapp.bean.MulitEditItemInfo;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<MulitEditItemInfo> list;

    public MyAdapter(List<MulitEditItemInfo> list) {
        this.list = list;
    }


    @Override
    public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
//        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mulit_edit, null);
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mulit_edit, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.tvNum.setText("序号："+list.get(position).getId());
        holder.tvName.setText("姓名："+list.get(position).getName());
        holder.etAge.setText(list.get(position).getAge()+"");
        holder.etaddress.setText(list.get(position).getAddress());
        editSetOnFocusChangeListener(holder.etAge, position, 0);
        editSetOnFocusChangeListener(holder.etaddress, position, 1);
    }

    //以下代码为重点
    private void editSetOnFocusChangeListener(EditText edt, int position, int i) {
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //删除.后面超过两位的数字
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        edt.setText(s);
                        edt.setSelection(s.length());
                    }
                }
                //如果.在起始位置,则起始位置自动补0
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    edt.setText(s);
                    edt.setSelection(2);
                }
                //如果起始位置为0并且第二位跟的不是".",则无法后续输入
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        edt.setText(s.subSequence(0, 1));
                        edt.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                switch (i) {
                    case 0:
                        if (s.length() > 0) {
                            list.get(position).setAge(Double.parseDouble(s.toString()));
                        } else {
                            list.get(position).setAge(0.00);
                        }
                        break;
                    case 1:
                        if (s.length() > 0) {
                            list.get(position).setAddress(s.toString());
                        } else {
                            list.get(position).setAddress("");
                        }
                        break;

                }
            }
        };

        edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    edt.addTextChangedListener(textWatcher);
                } else {
                    edt.removeTextChangedListener(textWatcher);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvNum;
        private TextView tvName;
        private EditText etAge;
        private EditText etaddress;

        public ViewHolder( View itemView) {
            super(itemView);
            tvNum = itemView.findViewById(R.id.tv_num);
            tvName = itemView.findViewById(R.id.tv_name);
            etAge = itemView.findViewById(R.id.et_age);
            etaddress = itemView.findViewById(R.id.et_address);
        }
    }
}